<template>
	<view class="container">
		<view class="intro"></view>
		<view class="bottom">
			<!-- #ifdef MP-WEIXIN -->
			<button type="primary" size="default" class="login-btn" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo">
				<image src="/static/images/mine/wechat.png"></image>
				微信一键登录
			</button>
			<!-- #endif -->
			<view class="d-flex flex-column justify-content-evenly align-items-center text-center" style="height: 30vh;">
				<view class="w-100 font-size-base text-color-assist">新用户登录即加入会员，享会员权益</view>
				<view class="font-size-base text-color-primary">会员权益说明</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				code: null
			};
		},
		onShow() {
			uni.login().then(res => {
				if (res[1]) {
					this.code = res[1].code;
				}
			});
		},
		methods: {
			...mapMutations(['SET_MEMBER', 'SET_TOKEN', 'SET_REFERRER', 'SET_USERID', 'SET_MOBILE']),
			...mapActions(['loginByWeiXin', 'userInfo']),
			async getUserInfo(e) {
				const {
					errMsg,
					userInfo
				} = e.detail;
				if (errMsg !== 'getUserInfo:ok') {
					uni.showModal({
						title: '提示',
						content: '您取消了授权登录，请重新授权',
						showCancel: false
					});
					uni.navigateBack();
					return;
				} else {
					uni.showLoading({
						mask: true
					});
					const pid = uni.getStorageSync('pid');
					let params = {
						code: this.code,
						referrer: pid,
						userInfo: e.detail
					};
					/*
					if (referrer) {
						params.referrer = referrer;
					}
					*/
					const result = await this.loginByWeiXin(params);
					uni.hideLoading();
					this.SET_USERID(result.data.userId);
					this.SET_MEMBER(result.data.userInfo);
					this.SET_TOKEN(result.data.token);
					this.SET_REFERRER(result.data.referrer);
					uni.navigateBack();
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.intro {
		width: 100%;
		height: 60vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-evenly;
		font-size: $font-size-base;
		color: $text-color-assist;

		image {
			width: 165rpx;
			height: 165rpx;
		}

		.tips {
			line-height: 72rpx;
			text-align: center;
		}
	}

	.bottom {
		height: 40vh;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 0 40rpx;

		.login-btn {
			width: 100%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;

			image {
				width: 36rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}

		.row {
			.grid {
				width: 20%;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
